<?php
/**
 * 商品列表控制器
 * 
 * 淘宝图片尺寸(从小到大)
 * 	_40x40.jpg
 * 	_sum.jpg
 * 	_100x100.jpg
 * 	_160x160.jpg
 * 	_b.jpg
 * 	_310x310.jpg
 * 
 * 淘宝商店图标的前缀为：http://logo.taobaocdn.com/shop-logo
 * 
 * @version $Id: Items.php 456 2012-10-08 08:04:37Z linsir123 $
 */
class controllerItems extends AppController 
{
	/**
	 * @var 模型
	 */
	public $mItem;
	
	/**
	 * @var 模型
	 */
	public $mParams;
	
	/**
	 * @var 模型
	 */
	public $mNav;
	
	/**
	 * @var 
	 */
	public $cId;
	
	/**
	 * @var 
	 */
	public $category;
	
	/**
	 * @var 
	 */
	public $type;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		parent::__construct();
		
		///
		$this->mParams = M::params();
		$this->mItem = M::item();
		$this->mNav = M::nav();
		
		///
		$this->_init();
		
		///
		AppView::addJs('colorbox/colorbox/jquery.colorbox.js');
		AppView::addCss('javascript/colorbox/example5/colorbox.css');
	}
	
	/**
	 * 淘宝推广页
	 */
	public function actionList()
	{
		AppView::addCss('themes/01/list.css');
		///
		$this->_display('list/index', 'getTaobaokes');
	}
	
	/**
	 * 淘宝推广页
	 */
	public function actionZhe()
	{
		AppView::addCss('themes/01/zhe.css');
		///
		$this->_display('zhe/index', 'getTaobaokesByCoupon');
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 初始化
	 */
	private function _init()
	{
		$cId = $this->mParams->getCid();
		$keyword = $this->mParams->getKeyword();
		$pinyin = $this->mParams->getPinyin();
		$this->type = $this->mParams->getType();
		
		///
		switch ($this->type) {
			/// 
			case 'zhe':
				App::V('pinyin');
				$pinyin2 = string2pinyin($keyword);
				if ($pinyin != $pinyin2) 
					$this->mParams->h301(array('pinyin'=>$pinyin2));
					
				break;
				
			///
			default:
				if ( ! empty($cId)) {
					if ($category = $this->mNav->getOne($cId)) {
						if ($pinyin != $category['pinyin']) 
							$this->mParams->h301(array('pinyin'=>$category['pinyin']));
					} else 
						$this->miss();
				} else 
					$category = array();
					
				///
				$this->cId = empty($category)?0:$cId;
				$this->category = $category;
				break;
		}
	}
	
	/**
	 * 输出
	 */
	private function _display($template, $method)
	{
		do {
			++$i;
			$callback = array(&$this->mItem, $method);
			$params = array($this->mParams->taobaoke, true, true);
			$data = call_user_func_array($callback, $params);
			list($list, $totalPage, $total) = $data;
			if ( ! empty($list)) 
				break;
				
		} while ($i <= 3);
		
		///
		View::display($template);
		View::assignSet(array(
			'location'=>$this->mNav->getLocation($this->cId, $this->type), 
			'category'=>$this->category,
			'cid'=>$this->cId,
			'total'=>$total, 
			'list'=>$list, 
		));
		$this->mParams->assign($totalPage, $this->category);
	}
}
